package dormouse.fx;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileType;
import org.apache.log4j.Logger;

/**
 * This class is an Observable wrapper around a file object.
 */
public class FileObjectRow {

	/** Class logger. */
	private static final Logger log = Logger.getLogger(FileObjectRow.class);

	/** The file name property. */
	private SimpleStringProperty name;

	/** A property that indicates if the file is hidden. */
	private SimpleBooleanProperty hidden;

	/** The file to be rendered. */
	private FileObject file;

	/** A property that indicates if the file is a directory. */
	private SimpleBooleanProperty isDirectory;

	/** The last modified time. */
	private SimpleLongProperty lastModifiedTime;

	/** The size of the file. */
	private SimpleLongProperty size;

	/**
	 * Constructor.
	 * 
	 * @param file
	 *            The file to be wrapped.
	 */
	public FileObjectRow(FileObject file) {
		this.file = file;
		this.name = new SimpleStringProperty(file.getName().getBaseName());

		try {
			this.hidden = new SimpleBooleanProperty(file.isHidden());
			this.isDirectory = new SimpleBooleanProperty(
					file.getType() == FileType.FOLDER);
			this.lastModifiedTime = new SimpleLongProperty(file.getContent()
					.getLastModifiedTime());

			if (!isDirectory.get()) {
				this.size = new SimpleLongProperty(file.getContent().getSize());
			}

		} catch (FileSystemException e) {
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * Gets the file name property.
	 * 
	 * @return the file name property
	 */
	public SimpleStringProperty name() {
		return name;
	}

	/**
	 * Gets a flag property used to indicate if the file is hidden.
	 * 
	 * @return a flag indicating that the file is hidden.
	 */
	public SimpleBooleanProperty hidden() {
		return hidden;
	}

	/**
	 * Gets a flag property used to indicate if the file is a directory.
	 * 
	 * @return a flag indicating that the file is a directory.
	 */
	public SimpleBooleanProperty isDirectory() {
		return isDirectory;
	}

	/**
	 * Gets a file size property.
	 * 
	 * @return the file size property
	 */
	public SimpleLongProperty size() {
		return size;
	}

	/**
	 * The last time that this file was modified.
	 * 
	 * @return the last modification time.
	 */
	public SimpleLongProperty lastModifiedTime() {
		return lastModifiedTime;
	}

	/**
	 * Gets the file that this row wraps.
	 * 
	 * @return the underlying file object
	 */
	public FileObject getFile() {
		return this.file;
	}

}
